
/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.order.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxk.oms.order.domain.OmsOrderItem;
import com.lxk.oms.order.domain.bo.OmsOrderItemBo;
import com.lxk.oms.order.domain.vo.OmsOrderItemVo;
import com.lxk.oms.order.mapper.OmsOrderItemMapper;
import com.lxk.oms.order.service.IOmsOrderItemService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 交易订单明细Service业务层处理
 *
 * @author LXK-RY5
 * @date 2024-08-20
 */
@RequiredArgsConstructor
@Service
public class OmsOrderItemServiceImpl implements IOmsOrderItemService {

    private final OmsOrderItemMapper baseMapper;

    /**
     * 查询IdList交易订单明细Map
     */
    @Override
    public Map<Long, OmsOrderItemVo> queryMapByIdList(List<Long> idList) {
        return this.queryByIdList(idList).stream().collect(Collectors.toMap(OmsOrderItemVo::getId, v -> v));
    }

    /**
     * 查询交易订单明细Map
     */
    @Override
    public Map<Long, OmsOrderItemVo> queryMap(OmsOrderItemBo bo) {
        return this.queryList(bo).stream().collect(Collectors.toMap(OmsOrderItemVo::getId, v -> v));
    }

    /**
     * 查询交易订单明细IdList
     */
    @Override
    public List<OmsOrderItemVo> queryByIdList(List<Long> idList) {
        if (CollUtil.isEmpty(idList)) {
            return CollUtil.newArrayList();
        }
        LambdaQueryWrapper<OmsOrderItem> lqw = Wrappers.lambdaQuery();
        lqw.in(OmsOrderItem::getId, idList);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 查询交易订单明细
     *
     * @param id 主键
     * @return 交易订单明细
     */
    @Override
    public OmsOrderItemVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询交易订单明细列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 交易订单明细分页列表
     */
    @Override
    public TableDataInfo<OmsOrderItemVo> queryPageList(OmsOrderItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OmsOrderItem> lqw = buildQueryWrapper(bo);
        Page<OmsOrderItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的交易订单明细列表
     *
     * @param bo 查询条件
     * @return 交易订单明细列表
     */
    @Override
    public List<OmsOrderItemVo> queryList(OmsOrderItemBo bo) {
        LambdaQueryWrapper<OmsOrderItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public List<OmsOrderItemVo> queryByOrderIdList(Long orderId) {
        return baseMapper.selectVoList(new LambdaQueryWrapper<OmsOrderItem>().eq(OmsOrderItem::getOrderId, orderId));
    }

    private LambdaQueryWrapper<OmsOrderItem> buildQueryWrapper(OmsOrderItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OmsOrderItem> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(OmsOrderItem::getId);
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            OmsOrderItem::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            OmsOrderItem::getUpdateTime, params.get("beginUpdateTime"), params.get("endUpdateTime"));
        lqw.eq(bo.getMerId() != null, OmsOrderItem::getMerId, bo.getMerId());
        lqw.eq(bo.getUserId() != null, OmsOrderItem::getUserId, bo.getUserId());
        lqw.eq(bo.getOrderId() != null, OmsOrderItem::getOrderId, bo.getOrderId());
        lqw.eq(bo.getSpuId() != null, OmsOrderItem::getSpuId, bo.getSpuId());
        lqw.like(StringUtils.isNotBlank(bo.getSpuName()), OmsOrderItem::getSpuName, bo.getSpuName());
        lqw.eq(bo.getSkuId() != null, OmsOrderItem::getSkuId, bo.getSkuId());
        lqw.like(StringUtils.isNotBlank(bo.getSkuName()), OmsOrderItem::getSkuName, bo.getSkuName());
        lqw.eq(StringUtils.isNotBlank(bo.getPicUrl()), OmsOrderItem::getPicUrl, bo.getPicUrl());
        lqw.eq(bo.getCount() != null, OmsOrderItem::getCount, bo.getCount());
        lqw.eq(bo.getPrice() != null, OmsOrderItem::getPrice, bo.getPrice());
        lqw.eq(bo.getTotalPrice() != null, OmsOrderItem::getTotalPrice, bo.getTotalPrice());
        lqw.eq(bo.getDiscountPrice() != null, OmsOrderItem::getDiscountPrice, bo.getDiscountPrice());
        lqw.eq(bo.getSaleAfterNum() != null, OmsOrderItem::getSaleAfterNum, bo.getSaleAfterNum());
        lqw.eq(bo.getSaleAfterStatus() != null, OmsOrderItem::getSaleAfterStatus, bo.getSaleAfterStatus());
        lqw.eq(bo.getOneCategoryId() != null, OmsOrderItem::getOneCategoryId, bo.getOneCategoryId());
        lqw.eq(bo.getTwoCategoryId() != null, OmsOrderItem::getTwoCategoryId, bo.getTwoCategoryId());
        lqw.eq(bo.getThreeCategoryId() != null, OmsOrderItem::getThreeCategoryId, bo.getThreeCategoryId());
        lqw.eq(bo.getOrderType() != null, OmsOrderItem::getOrderType, bo.getOrderType());
        return lqw;
    }

    /**
     * 新增交易订单明细
     *
     * @param bo 交易订单明细
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(OmsOrderItemBo bo) {
        OmsOrderItem add = MapstructUtils.convert(bo, OmsOrderItem.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改交易订单明细
     *
     * @param bo 交易订单明细
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(OmsOrderItemBo bo) {
        OmsOrderItem update = MapstructUtils.convert(bo, OmsOrderItem.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OmsOrderItem entity) {
    }

    /**
     * 校验并批量删除交易订单明细信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
